package com.yuncheng.spcyApi.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yuncheng.spcyApi.dto.JcyLxqdccDto;
import com.yuncheng.spcyApi.entity.SpcyJcyLxqdcs;
import com.yuncheng.spcyApi.entity.SpcyJcyLxqdjl;
import com.yuncheng.spcyApi.mapper.SpcyJcyLxqdcsMapper;
import com.yuncheng.spcyApi.mapper.SpcyJcyLxqdjlMapper;
import com.yuncheng.spcyApi.service.ISpcyJcyLxqdcsService;
import com.yuncheng.spcyApi.utils.GetCurrentUserUtils;
import com.yuncheng.spcyApi.utils.GetUuIdUtils;
import com.yuncheng.spcyApi.vo.jcy.JcyLxqdjlVo;
import com.yuncheng.spcyApi.vo.common.OrderItemVo;
import com.yuncheng.spcyApi.vo.common.PageVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;

import com.yuncheng.common.base.service.impl.BaseServiceImpl;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Optional;

/**
 * @Description: 检查员遴选签到场次
 * @Author: yuncheng
 * @Date: 2024-01-11
 * @Version: V1.0
 */
@Service
public class SpcyJcyLxqdcsServiceImpl extends BaseServiceImpl<SpcyJcyLxqdcsMapper, SpcyJcyLxqdcs> implements ISpcyJcyLxqdcsService {

    @Resource
    @Lazy
    private SpcyJcyLxqdjlMapper spcyJcyLxqdjlMapper;

    @Override
    public Page<JcyLxqdjlVo> selectLxqdjlPageList(PageVo pageVo, JcyLxqdjlVo bean, OrderItemVo orderItemVo) {
        Page<JcyLxqdjlVo> page = new Page<>(pageVo.getPageNo(), pageVo.getPageSize());
        return spcyJcyLxqdjlMapper.selectLxqdjlPageList(page, bean, orderItemVo);
    }

    @Override
    public List<SpcyJcyLxqdjl> selectLxqdjlByUser(String flxjhid, String userid) {
        return spcyJcyLxqdjlMapper.selectList(new LambdaQueryWrapper<SpcyJcyLxqdjl>()
                .eq(SpcyJcyLxqdjl::getFlxjhid, flxjhid)
                .eq(SpcyJcyLxqdjl::getFuserid, userid));
    }

    @Override
    public List<SpcyJcyLxqdcs> selectLxqdccList(String flxjhid) {
        return list(new LambdaQueryWrapper<SpcyJcyLxqdcs>()
                .eq(SpcyJcyLxqdcs::getFlxjhid, flxjhid)
                .orderByAsc(SpcyJcyLxqdcs::getCreateTime));
    }

    @Override
    public void initGenQdjl(String fuserid, String flxjhid, String flxqdccid) {

        boolean isQdrsFlag = false; // 签到人数
        List<SpcyJcyLxqdjl> qdjlList = spcyJcyLxqdjlMapper.selectQdjlByUserId(fuserid, flxqdccid);
        if (CollectionUtil.isNotEmpty(qdjlList)){
            // 签到时间
            if (qdjlList.get(0).getFqdsj() == null) {
                isQdrsFlag = true;

                LambdaUpdateWrapper<SpcyJcyLxqdjl> updateWrapper = new LambdaUpdateWrapper<>();
                updateWrapper.eq(SpcyJcyLxqdjl::getId, qdjlList.get(0).getId());

                SpcyJcyLxqdjl updateQdjl = new SpcyJcyLxqdjl();
                updateQdjl.setFqdsj(new Date());
                updateQdjl.setUpdateTime(new Date());
                updateQdjl.setUpdateBy(GetCurrentUserUtils.getCurrentUserAccount());
                spcyJcyLxqdjlMapper.update(updateQdjl, updateWrapper);
            }
        }else {
            isQdrsFlag = true;

            SpcyJcyLxqdjl jcyLxqdjl = new SpcyJcyLxqdjl();
            jcyLxqdjl.setId(GetUuIdUtils.ReplaceUuId());
            jcyLxqdjl.setFlxjhid(flxjhid);
            jcyLxqdjl.setFuserid(fuserid);
            jcyLxqdjl.setFlxqdccid(flxqdccid);
            jcyLxqdjl.setFqdsj(new Date());
            jcyLxqdjl.setCreateTime(new Date());
            jcyLxqdjl.setUpdateTime(new Date());
            jcyLxqdjl.setCreateBy(GetCurrentUserUtils.getCurrentUserAccount());
            jcyLxqdjl.setUpdateBy(GetCurrentUserUtils.getCurrentUserAccount());
            spcyJcyLxqdjlMapper.insert(jcyLxqdjl);
        }

        if (isQdrsFlag) {
            addSyncQdcsYqdrs(flxqdccid);
        }

    }

    @Override
    public void initGenQdjlInShtg(String fuserid, String flxqdccid, String fbz, Date fqdsj, boolean isUpdateBz) {
        SpcyJcyLxqdcs qdcs = getById(flxqdccid);
        if (qdcs == null){
            return;
        }

        List<SpcyJcyLxqdjl> qdjlList = spcyJcyLxqdjlMapper.selectQdjlByUserId(fuserid, flxqdccid);
        if (CollectionUtil.isNotEmpty(qdjlList)) {
            LambdaUpdateWrapper<SpcyJcyLxqdjl> updateWrapper = new LambdaUpdateWrapper<>();
            updateWrapper.eq(SpcyJcyLxqdjl::getId, qdjlList.get(0).getId());

            SpcyJcyLxqdjl updateQdjl = new SpcyJcyLxqdjl();
            if (isUpdateBz) {
                updateQdjl.setFbz(fbz);
                updateQdjl.setFqdsj(fqdsj);
            }else {
                updateQdjl.setFqdsj(fqdsj);
            }
            updateQdjl.setUpdateTime(new Date());
            updateQdjl.setUpdateBy(GetCurrentUserUtils.getCurrentUserAccount());
            spcyJcyLxqdjlMapper.update(updateQdjl, updateWrapper);
        }else {

            SpcyJcyLxqdjl jcyLxqdjl = new SpcyJcyLxqdjl();
            jcyLxqdjl.setId(GetUuIdUtils.ReplaceUuId());
            jcyLxqdjl.setFlxjhid(qdcs.getFlxjhid());
            jcyLxqdjl.setFuserid(fuserid);
            jcyLxqdjl.setFlxqdccid(flxqdccid);
            if (isUpdateBz) {
                jcyLxqdjl.setFbz(fbz);
                jcyLxqdjl.setFqdsj(fqdsj);
            }else {
                jcyLxqdjl.setFqdsj(fqdsj);
            }
            jcyLxqdjl.setCreateTime(new Date());
            jcyLxqdjl.setUpdateTime(new Date());
            jcyLxqdjl.setCreateBy(GetCurrentUserUtils.getCurrentUserAccount());
            jcyLxqdjl.setUpdateBy(GetCurrentUserUtils.getCurrentUserAccount());
            spcyJcyLxqdjlMapper.insert(jcyLxqdjl);
        }


    }

    @Override
    public List<JcyLxqdccDto> selectLxqdccList(String fuserid, String flxjhid) {
        return spcyJcyLxqdjlMapper.selectLxqdccList(flxjhid, fuserid);
    }

    @Override
    public void deleteLxpxqdcs(String flxqdccid) {
        removeById(flxqdccid);

        spcyJcyLxqdjlMapper.delete(new LambdaQueryWrapper<SpcyJcyLxqdjl>()
                .eq(SpcyJcyLxqdjl::getFlxqdccid, flxqdccid));
    }

    @Override
    public void deleteLxpxqdcsByLxjdId(String flxjhid) {
        remove(new LambdaQueryWrapper<SpcyJcyLxqdcs>()
                .eq(SpcyJcyLxqdcs::getFlxjhid, flxjhid));

        spcyJcyLxqdjlMapper.delete(new LambdaQueryWrapper<SpcyJcyLxqdjl>()
                .eq(SpcyJcyLxqdjl::getFlxjhid, flxjhid));
    }

    @Override
    public List<SpcyJcyLxqdjl> selectLxqdcsQdjl(String flxqdccid) {
        return spcyJcyLxqdjlMapper.selectList(new LambdaQueryWrapper<SpcyJcyLxqdjl>()
                .eq(SpcyJcyLxqdjl::getFlxqdccid, flxqdccid));
    }

    @Override
    public void addSyncQdcsYqdrs(String flxqdccid) {
        if (StringUtils.isBlank(flxqdccid)){
            return;
        }

        SpcyJcyLxqdcs qdcs = getById(flxqdccid);
        if (qdcs == null){
            return;
        }

        int fyqdrs = Optional.ofNullable(qdcs.getFyqdrs()).orElseGet(()->0);
        qdcs.setFyqdrs(fyqdrs+1);
        updateById(qdcs);
    }

}
